<?php
/**
 * 分类目录管理
 * @author cjli
 *
 */
class VocabularyAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function index()
	{
	  $vocdb = D('vocabulary');
	  $vocabularyList = $vocdb->select();
	  $this->assign('vocabularyList', $vocabularyList);
		$this->display('list');
	}
	
	public function add()
	{
		$vocdb = D('vocabulary');
		if(isset($_POST['dosubmit'])){
			unset($_POST['dosubmit']);
			$vocdb->create();
			$lastid = $vocdb->add();
			if($lastid){
				S('admin_vocabulary_list', null);
				$this->success('分类目录添加成功！', U('vocabulary/index'), 'add');
			}else{
				$this->error('分类目录添加失败！', U('vocabulary/index'), 'add');
			}
		}else{
			$this->display('add');
		}
	}
	
	public function edit()
	{
		$vocdb = D('vocabulary');
		if(isset($_POST['dosubmit'])){
			unset($_POST['dosubmit']);
			$vocdb->create();
			$vocdb->save();
			S('admin_vocabulary_list', null);
			$this->success('分类目录编辑成功！', U('vocabulary/index'), 'edit');
		}else{
			$vid = intval($_GET['vid']);
			$vinfo = $vocdb->where('id='.$vid)->find();
			$this->assign('vinfo', $vinfo);
			$this->display('edit');
		}
	}
	
	public function delete()
	{
		$vid = intval($_GET['vid']);
		$vocdb = D('vocabulary');
	  $vocdb->delete($vid);
	  S('admin_vocabulary_list', null);
		$this->success('分类目录删除成功！');
	}
	
	//ajax检验分类目录是否已经存在
	public function public_checkname_ajx()
	{
		$name = trim($_GET['name']);
		$vid = intval($_GET['vid']);
		$vocdb = D('vocabulary');
		$where['name'] = $name;
		if($vid){
			$where['id'] = array('neq',$vid);
		}
		$rs = $vocdb->where($where)->getField('id');
		if(is_null($rs)){
			exit('1');
		}
		exit('0');
	}
}